#pragma once
#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>
#include"Log.hpp"


class Util{
  public:
    //按行读取
    static int GetHttpLine(int sock,std::string& strline)
    {
      char ch = 'a';
      while(ch != '\n')
      {
        ssize_t sz = recv(sock,&ch,1,0);
        if(sz > 0)
        {
          //如果换行是/r开头的
          if(ch == '\r')
          {
            recv(sock,&ch,1,MSG_PEEK);
            //如果换行是\r\n
            if(ch == '\n')
            {
              //去掉\r，保持统一的\n
              recv(sock,&ch,1,0);
            }
            //换为统一的\n
            else{
              ch = '\n';
            }
          }
          strline += ch;
        }
        else if(sz == 0)
        {
          return 0;
        }
        else{
          return -1;
        }
      }
      return strline.size();
    }
    static void CutString(std::string &str_left,std::string &str_right,const std::string key,const std::string str_all)
    {
      size_t index = str_all.find(key);
      if(index == str_all.npos)
      {
        Log::LOG(WARNING,"CutString fail");        
        return;
      }

      str_left = str_all.substr(0,index);
      str_right = str_all.substr(index+key.size());
    }
};
